package com.vdg.lockphotos.gridphotos;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;

import com.vdg.lockphotos.R;
import com.vdg.lockphotos.data.Photo;
import com.vdg.lockphotos.notify.OnLoadFileListner;
import com.vdg.lockphotos.util.ImageLoader;
import com.vdg.lockphotos.util.Utilities;
import com.vdg.lockphotos.view.PhotoLoadingView;

public class GridHiddenPhotoAdapter extends ArrayAdapter<Photo> {
	private ArrayList<Photo> mListPhoto;
	private Context mContext;
	private ImageLoader mImageLoader;
	private int mHeight;
	private boolean mSelectAble;

	public GridHiddenPhotoAdapter(Context context, int textViewResourceId,
			ArrayList<Photo> objects) {
		super(context, textViewResourceId, objects);
		mListPhoto = objects;
		mContext = context;
		mHeight = getHeight(context);
		mImageLoader = ImageLoader.getInstance(getContext());
	}

	public static int getHeight(Context activity) {
		WindowManager wm = (WindowManager) activity
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		return (int) ((Utilities.getDisplaySize(display).x - Utilities.dpToPx(
				activity, 20)) / 3);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final PhotoLoadingView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			imageView = new PhotoLoadingView(getContext());
			imageView.setLayoutParams(new AbsListView.LayoutParams(
					AbsListView.LayoutParams.FILL_PARENT, mHeight));
		} else {
			imageView = (PhotoLoadingView) convertView;
		}
		// imageView.setImageURI( Uri.fromFile(new
		// File(mArrayEmodata[position].getPathFile())));

		mImageLoader.DisplayImage(mListPhoto.get(position).getHiddenUrl(),
				imageView.mImvPhoto, new OnLoadFileListner() {

					@Override
					public void onSuccess(boolean isAnimation) {
						// if (isAnimation) {
						// mImvPhoto.startAnimation(myFadeInAnimation);
						// }
					}

					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						imageView.mImvPhoto.setImageDrawable(mContext
								.getResources().getDrawable(
										R.drawable.white_drawable));
					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub
						imageView.mImvPhoto.setImageDrawable(mContext
								.getResources().getDrawable(
										R.drawable.white_drawable));
					}
				}, mHeight / 2, true);
		if (mSelectAble) {
			if (mListPhoto.get(position).isIsSelected()) {
				imageView.mImvDefaultPhoto.setVisibility(View.VISIBLE);
				imageView.mImvDefaultPhoto.setBackground(mContext
						.getResources().getDrawable(R.drawable.selected_bg));
				imageView.mImvDefaultPhoto.setImageDrawable(mContext
						.getResources().getDrawable(R.drawable.ic_selected));
			} else {
				imageView.mImvDefaultPhoto.setBackground(mContext
						.getResources().getDrawable(R.drawable.selectable));
				imageView.mImvDefaultPhoto
						.setImageResource(android.R.color.transparent);
				imageView.mImvDefaultPhoto.setVisibility(View.VISIBLE);
			}
		} else {
			if (mListPhoto.get(position).isIsSelected()) {
				imageView.mImvDefaultPhoto.setVisibility(View.VISIBLE);
			} else {
				imageView.mImvDefaultPhoto.setVisibility(View.GONE);
			}
		}
		return imageView;
	}

	public ArrayList<Photo> getSelectedData() {
		ArrayList<Photo> listResult = new ArrayList<Photo>();
		for (Photo data : mListPhoto) {
			if (data.isIsSelected()) {
				listResult.add(data);
			}
		}
		return listResult;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListPhoto.size();
	}

	public boolean isSelectAble() {
		return mSelectAble;
	}

	public void setSelectAble(boolean mSelectAble) {
		this.mSelectAble = mSelectAble;
	}

}
